package org.example.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import jakarta.annotation.Resource;
import jakarta.websocket.server.PathParam;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.example.exception.BlockExceptionPlanB;
import org.example.service.ConsumerService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Auther:Liu
 * @Testname:ConsumerController
 * @Date:2025/9/10 14:44
 */
@Slf4j
@RestController
@RequestMapping("/consumer")
public class ConsumerController {
    @Resource
    private ConsumerService consumerService;

    @GetMapping("/a")
    String A(){
        return consumerService.A();
    }

    @GetMapping("/b")
    String B(){
        return consumerService.B();
    }

    @GetMapping("/c")
    String C(){
        return "This is a consumer`c";
    }

    @GetMapping("/d1")
    String D1(){
        return consumerService.D();
    }

    @GetMapping("/d2")
    String D2(){
        return consumerService.D();
    }

    @GetMapping("/e")
    @SneakyThrows
    String E(){
//        Thread.sleep(2000);
        return "This is a consumer`e";
    }

    @GetMapping("/f")
    String F(){
        int a=9/0;
        return "This is a consumer`f";
    }

    @GetMapping("/g")
    @SentinelResource("messageG")
    String G(@RequestParam("name") String name, @RequestParam("age")String age){
        return "This is a consumer`g"+name+":"+age;
    }

    @GetMapping("/h")
    public String h(@RequestParam("origin") String origin){
        return "This is SentinelConsumerController`h";
    }

    @GetMapping("/i")
//      @SentinelResource(value = "messageI", blockHandler = "planB")
    @SentinelResource(value = "messageI",
            blockHandler = "abc", blockHandlerClass = BlockExceptionPlanB.class,
            fallback = "ccc", fallbackClass = BlockExceptionPlanB.class)
    public String i(@RequestParam(defaultValue = "1", value = "num") int num) {
        log.info("用户执行了i方法");
        int a = 9 / num;
        return "This is SentinelConsumerController`i";
    }

    public String planB(int num, BlockException e) {
        log.info("执行了planB方案");
        return "This is SentinelConsumerController`planB";
    }

    public String planC(int num, Throwable e) {
        return "This is SentinelConsumerController`planC";
    }
}
